<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编程导航后台注册</title>
    <link rel="stylesheet" href="css/style.css">
    <style>
        :root {
            --primary-color: #4285f4;
            --secondary-color: #f1f3f4;
            --text-color: #202124;
            --border-radius: 8px;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f5f5f5;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .container {
            background-color: white;
            padding: 2rem;
            border-radius: var(--border-radius);
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
        }

        h2 {
            color: var(--primary-color);
            text-align: center;
            margin-bottom: 1.5rem;
        }

        .form-group {
            margin-bottom: 1.2rem;
        }

        label {
            display: block;
            margin-bottom: 0.5rem;
            color: var(--text-color);
            font-weight: 500;
        }

        input[type="text"],
        input[type="password"],
        input[type="file"] {
            width: 100%;
            padding: 0.8rem;
            border: 1px solid #ddd;
            border-radius: var(--border-radius);
            box-sizing: border-box;
            transition: border-color 0.3s;
        }

        input[type="text"]:focus,
        input[type="password"]:focus {
            outline: none;
            border-color: var(--primary-color);
        }

        button {
            background-color: var(--primary-color);
            color: white;
            border: none;
            padding: 0.8rem 1.5rem;
            border-radius: var(--border-radius);
            cursor: pointer;
            width: 100%;
            font-size: 1rem;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #3367d6;
        }
    </style>
</head>
<!--引入vue相关的js文件-->
<script src="./js/vue.js"></script>
<script src="./js/axios.min.js"></script>
<body>
<div class="container" id="app">
    <h2>注册</h2>
    <form id="registerForm" action="/pns/register" method="post" enctype="multipart/form-data">
        <div class="form-group">
            <label for="regUsername">用户名</label>
            <input v-model="registerData.username" type="text" id="regUsername" name="username" required>
        </div>
        <div class="form-group">
            <label for="regPassword">密码</label>
            <input v-model="registerData.pwd" type="password" id="regPassword" name="pwd" required>
        </div>
        <div class="form-group">
            <label for="avatar">上传头像</label>
            <input type="file" id="avatar" name="headImg" @change="handleFileChange">
        </div>
        <button @click="submit($event)">注册</button>
    </form>
</div>
<script>
    new Vue({
        el: "#app",
        data: {
            registerData: {
                username: '',
                pwd: '',
                headImg: ''
            }
        },
        methods: {
            submit: function (event) {
                event.preventDefault();
                const formData = new FormData();
                formData.append('username', this.registerData.username);
                formData.append('pwd', this.registerData.pwd);
                if (this.registerData.headImg) {
                    formData.append('headImg', this.registerData.headImg);
                }
                axios({
                    url: '/pns/register',
                    method: 'post',
                    data: formData,
                    headers: { 'Content-Type': 'multipart/form-data' }
                }).then(result => {
                    if (result.status === 200) {
                        location.href = 'login.html';
                    }
                }).catch(error => {
                    console.error('注册失败:', error);
                });
            },
            handleFileChange: function (event) {
                this.registerData.headImg = event.target.files[0];
            }
        }
    });
</script>

</body>
</html>